package com.example.normaldemo.dragVIew;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;

import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.AppCompatSeekBar;

public class RulerSeekBar extends AppCompatSeekBar {

    /**
     * 刻度线画笔
     */
    private Paint mRulerPaint;

    /**
     * 分割条画笔
     */
    private Paint mDividerPaint;

    /**
     * 刻度线的个数,等分数等于刻度线的个数加1
     */
    private int mRulerCount = 4;

    /**
     * 每条刻度线的宽度
     */
    private int mRulerWidth = 10;

    //分割刻度的宽高
    private int mDividerHeight = 40, mDividerWidth = 20;

    /**
     * 刻度线的颜色
     */
    private int mRulerColor = Color.WHITE;

    /**
     * 滑块上面是否要显示刻度线
     */
    private boolean isShowTopOfThumb = false;

    //触摸事件 down事件的起始位置
    private float mStartX, mStartY;

    private int mTouchSlop;

    //单个刻度的宽度
    private float mSingleLevelWidth;

    //是否调用了滑动事件
    private boolean mHasMove;

    public RulerSeekBar(Context context) {
        super(context);
        init(context);
    }

    public RulerSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public RulerSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * 初始化
     */
    private void init(Context context) {
        //创建绘制刻度线的画笔
        mRulerPaint = new Paint();
        mRulerPaint.setColor(mRulerColor);
        mRulerPaint.setAntiAlias(true);

        mDividerPaint = new Paint();
        mDividerPaint.setColor(Color.parseColor("#03DAC5"));
        mDividerPaint.setAntiAlias(true);

        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

        //Api21及以上调用，去掉滑块后面的背景
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            setSplitTrack(false);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.mSingleLevelWidth = (getWidth() - getPaddingLeft() - getPaddingRight()) / (mRulerCount + 1);
    }

    /**
     * 重写onDraw方法绘制刻度线
     *
     * @param canvas
     */
    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //极限条件校验
        if (getWidth() <= 0 || mRulerCount <= 0) {
            return;
        }

        //获取每一份的长度
        int length = (getWidth() - getPaddingLeft() - getPaddingRight() - mRulerCount * mRulerWidth) / (mRulerCount + 1);

        //计算刻度线的顶部坐标和底部坐标
        int rulerTop = getHeight() / 2 - getMinimumHeight() / 2;
        int rulerBottom = rulerTop + getMinimumHeight();

        //获取滑块的位置信息
        Rect thumbRect = null;
        if (getThumb() != null) {
            thumbRect = getThumb().getBounds();
        }

        //绘制刻度线
        for (int i = 0; i <= mRulerCount + 1; i++) {
            //计算刻度线的左边坐标和右边坐标
            int rulerLeft = i * length + getPaddingLeft();
            int rulerRight = rulerLeft + mRulerWidth;

            //判断是否需要绘制刻度线
            if (!isShowTopOfThumb && thumbRect != null && rulerLeft - getPaddingLeft() > thumbRect.left && rulerRight - getPaddingLeft() < thumbRect.right) {
                continue;
            }

            drawDividerRect(canvas, i);

            //进行绘制
//            if (i > 0 && i < mRulerCount + 1)
//                canvas.drawRect(rulerLeft, rulerTop, rulerRight, rulerBottom, mRulerPaint);
        }
    }


    private void drawDividerRect(Canvas canvas, int index) {
        float left = (index * mSingleLevelWidth) - mDividerWidth / 2 + getPaddingLeft();
        if (left < getPaddingLeft()) {
            left = getPaddingLeft();
        } else if (left > getWidth() - mDividerWidth - getPaddingRight()) {
            left = getWidth() - mDividerWidth - getPaddingRight();
        }
        float right = left + mDividerWidth;
        float top = getHeight() / 2 - mDividerHeight / 2;
        float bottom = top + mDividerHeight;


        canvas.drawRect(left, top, right, bottom, mDividerPaint);
    }

    //处理点击事件 点击后 向离当前值最近的刻度靠拢
    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                float endMoveX = event.getX();

                if (Math.abs(endMoveX - mStartX) > mTouchSlop) {
                    mHasMove = true;
                }

                break;
            case MotionEvent.ACTION_DOWN:
                //获取屏幕上点击的坐标
                mHasMove = false;
                mStartX = event.getX();
                mStartY = event.getY();
                break;
            case MotionEvent.ACTION_UP:
                //点击抬起后，回复初始位置。
                float endX = event.getX();
                float desX = Math.abs(endX - mStartX);

                if (desX < mTouchSlop && !mHasMove) {
                    //是点击事件
                    //计算当前刻度
                    int currProgress = (int) (Math.round((endX - getPaddingLeft()) / mSingleLevelWidth) * (getMax() / (mRulerCount + 1)));

                    setProgress(currProgress, true);
                } else {
                    //超过最小滑动距离  是滑动事件
                }

                return true;
        }
        return super.onTouchEvent(event);
    }

    /**
     * 设置刻度线的个数
     *
     * @param mRulerCount
     */
    public void setRulerCount(int mRulerCount) {
        this.mRulerCount = mRulerCount;
        requestLayout();
    }

    /**
     * 设置刻度线的宽度，单位(px)
     *
     * @param mRulerWidth
     */
    public void setRulerWidth(int mRulerWidth) {
        this.mRulerWidth = mRulerWidth;
        requestLayout();
    }

    /**
     * 设置刻度线的颜色
     *
     * @param mRulerColor
     */
    public void setRulerColor(int mRulerColor) {
        this.mRulerColor = mRulerColor;
        if (mRulerPaint != null) {
            mRulerPaint.setColor(mRulerColor);
            requestLayout();
        }
    }

    /**
     * 滑块上面是否需要显示刻度线
     *
     * @param isShowTopOfThumb
     */
    public void setShowTopOfThumb(boolean isShowTopOfThumb) {
        this.isShowTopOfThumb = isShowTopOfThumb;
        requestLayout();
    }
}
